package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;

import net.tsz.afinal.FinalBitmap;

import java.util.List;

/**
 * @author sbk
 * @createdate 2013-12-15 下午2:08:22
 * @Description: 答题适配器
 */
public class InteractionAnswerGdAdapter extends BaseAdapter {
	private Context con;
	private List<String> aListItem;
	private String type;
	private LinearLayout.LayoutParams lp;
	private Handler mhandler;
	private FinalBitmap fb;
	private final String IMAGE_TYPE="1";
	private final String WORDS_TYPE="2";

	public InteractionAnswerGdAdapter(Context con, Handler mhandler) {
		super();
		this.con = con;
		this.mhandler = mhandler;
		lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		// 此处相当于布局文件中的Android:layout_gravity属性
		lp.gravity = Gravity.RIGHT;
		fb = FinalBitmap.create(con);
	}

	public void setaListItem(List<String> aListItem, String type) {
		this.aListItem = aListItem;
		this.type = type;
	}

	@Override
	public int getCount() {
		//Auto-generated method stub
		return aListItem.size();
	}

	@Override
	public Object getItem(int position) {
		//Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		//Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LinearLayout layout = (LinearLayout) LayoutInflater.from(con).inflate(
				R.layout.interaction_answer_layout_gd_item, null);
		LinearLayout l1 = (LinearLayout) layout.findViewById(R.id.l1);
		RelativeLayout r1 = (RelativeLayout) layout.findViewById(R.id.rely);
		ImageView img = (ImageView) layout.findViewById(R.id.iv1);
		TextView tv = (TextView) layout.findViewById(R.id.text1);
		TextView btn = (TextView) layout.findViewById(R.id.bt_id);

		if (IMAGE_TYPE.equals(type)) { // 图片
			fb.display(img, aListItem.get(position));
		} else if (WORDS_TYPE.equals(type)) { // 文字
			l1.setOrientation(LinearLayout.HORIZONTAL);
			r1.setVisibility(View.GONE);
			tv.setText(aListItem.get(position));
			tv.setVisibility(View.VISIBLE);
			btn.setLayoutParams(lp);
		}
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Message msg = new Message();
				msg.what = 1;
				msg.arg1 = position;
				mhandler.sendMessage(msg);
			}
		});
		return layout;
	}

}
